package com.wythe.mall.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wythe.mall.R;
import com.wythe.mall.adapter.ImageShopAdapter;
import com.wythe.mall.intf.OnRequestDataListener;
import com.wythe.mall.model.ShopBean;
import com.wythe.mall.utils.ApiOkHttp;
import com.wythe.mall.utils.GotoActivity;
import com.wythe.mall.view.NoScrollGridView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class PayCompleteActivity extends AppCompatActivity {
    private TextView mTitle;
    private NoScrollGridView mGridView;
    private ImageShopAdapter mShopAdapter;
    private List<ShopBean> list;
    private int shopId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_complete);
        if (list == null) {
            list = new ArrayList<>();
        }
        list.clear();
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    public void initView() {
        findViewById(R.id.title_left_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        mGridView = (NoScrollGridView) findViewById(R.id.iv_pay_completed);
        mShopAdapter = new ImageShopAdapter(this, list);
        mGridView.setAdapter(mShopAdapter);
        mTitle = (TextView) findViewById(R.id.titleText);
        mTitle.setText("支付成功");
        findViewById(R.id.tv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                GotoActivity.gotoActiviy(PayCompleteActivity.this, MainActivity.class);
            }
        });

        findViewById(R.id.tv_order).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                GotoActivity.gotoActiviy(PayCompleteActivity.this, OrderActivity.class);
            }
        });
    }

    private void initData() {
        JSONObject param_last = new JSONObject();
        try {
            param_last.put("position",3);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        ApiOkHttp.getClassLabel(this,param_last, new OnRequestDataListener() {
            @Override
            public void requestSuccess(int code, JSONObject data) {
                if (code == 200) {
                    try {
                        JSONArray jsonArray = data.getJSONArray("data");
                        shopId = jsonArray.getJSONObject(0).getInt("id");
                        getShopList(shopId);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void requestFailure(int code, String msg) {

            }
        });

    }

    public void getShopList(int type) {
        JSONObject params = new JSONObject();
        try {
            params.put("indexLabelId", type);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiOkHttp.getShopList(this, params, new OnRequestDataListener() {
            @Override
            public void requestSuccess(int code, JSONObject data) {
                if (list == null) {
                    list = new ArrayList<>();
                }
                list.clear();
                try {
                    JSONArray array = data.getJSONArray("data");
                    for (int i = 0; i < array.length(); i++) {
                        ShopBean bean = new Gson().fromJson(array.getJSONObject(i).toString(), ShopBean.class);
                        list.add(bean);
                    }
                    mShopAdapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void requestFailure(int code, String msg) {

            }
        });

    }
}
